$(document).ready(function() {
    if (typeof(Storage) !== "undefined")
    {
        
        $('.wd-loading').show();
        getGuestList();

        //paging
        var killScroll = false;
        $(window).scroll(function() {
            if ($('#end_paging').val() == 0) {
                if ($(window).scrollTop() > ($(document).height() - $(window).height() - 100)) {
                    if (killScroll == false) {
                        killScroll = true;
                        $.post(localStorage.domain_app+"/webservice/guest/getEventGuestList.php", {
                            page: $('#page').val(),
                            event_id: sessionStorage.event_edit_id,
                            user_id: localStorage.userID
                        }, function(res) {
                            $('#wd-list-guest-event').append(res.html);
                            $('#page').val(res.page);
                            $('#end_paging').val(res.end_paging);
                            checkAll();
                            killScroll = false;
                        }, "json");
                    } else {
                        return false;
                    }
                }
            }
        });
        var request_address = false;
        $('#request-address-guest').click(function() {
            if (request_address == false) {
                request_address = true;
                var guest_list_from_fb = "";
                var guest_list_from_email = "";
                var count = 0;
                var check_guest = false;
                $('.checkbox-request-address-guest-by-facebook').each(function() {
                    if ($(this).is(':checked')) {
                        check_guest = true;
                        if (count == 0) {
                            guest_list_from_fb = $(this).attr('rel');
                        } else {
                            guest_list_from_fb = guest_list_from_fb + "," + $(this).attr('rel');
                        }
                        count++;
                    }
                });
                count = 0;
                $('.checkbox-request-address-guest-by-email').each(function() {
                    if ($(this).is(':checked')) {
                        check_guest = true;
                        if (count == 0) {
                            guest_list_from_email = $(this).attr('rel');
                        } else {
                            guest_list_from_email = guest_list_from_email + "," + $(this).attr('rel');
                        }

                        count++;
                    }
                });
                if (check_guest) {
                    sessionStorage.guest_list_from_fb = guest_list_from_fb;
                    sessionStorage.guest_list_from_email = guest_list_from_email;
                    wd_redirect("createMessageRequestAddress.html");
                } else {
                    print_alert("Please select guest(s)!");
                }
                request_address = false;
            }
        });


    }
});
function checkAll() {
    $('.checkall-fb').click(function() {
        var checkbox_all = $('#' + $(this).attr('for')).is(':checked');
        if (checkbox_all) {
            $('#wd-list-guest-event .checkbox-request-address-guest-by-facebook').removeAttr('checked');
        } else {
            $('#wd-list-guest-event .checkbox-request-address-guest-by-facebook').attr('checked', 'checked');
        }

    });
    $('.checkall-email').click(function() {
        var checkbox_all = $('#' + $(this).attr('for')).is(':checked');
        if (checkbox_all) {
            $('#wd-list-guest-event .checkbox-request-address-guest-by-email').removeAttr('checked');
        } else {
            $('#wd-list-guest-event .checkbox-request-address-guest-by-email').attr('checked', 'checked');
        }

    });
}
function getGuestList() {
    $('#wd-checkall-fb').removeAttr('checked');
    $('#wd-checkall-email').removeAttr('checked');
    $.post(localStorage.domain_app+"/webservice/guest/getEventGuestList.php", {
        page: 1,
        event_id: sessionStorage.event_edit_id,
        user_id: localStorage.userID
    }, function(res) {
        $('#wd-list-guest-event').html(res.html);
        $('#page').val(res.page);
        $('#end_paging').val(res.end_paging);
        $('.wd-loading').hide();
        checkAll();
    }, "json");
}
